<?php

namespace App\Http\Controllers;

use App\Models\Tag;
use App\Models\Area;
use App\Models\Column;
use App\Models\Site;

class SiteController extends Controller
{
    public function tag($name)
    {
        $col    = Tag::where('name',$name)->first();
        if(!$col){
            return redirect('site', 301);
        }
        $sites  = Site::where('keywords','like','%'.$name.'%')->where(['state'=>1])->orderBy('updated_at', 'desc')->paginate(10);
        return view('tag.list',compact('sites','col'));
    }
    //
    public function area($id)
    {
        $col = Area::where('name', $id)->first();
        if(!$col){
            return redirect('site', 301);
        }
        $sites  = Site::where(['state'=>1,'area_id'=> $col->id])->orderBy('updated_at', 'desc')->paginate(10);
        return view('site.area',compact('col','sites'));
    }
    public function index()
    {
        $col = Column::where('parent_id',0)->where('type',0)->get();
        return view('site.index',compact('col'));
    }
    public function listId($id)
    {
        $nav_id = $id;
        $col    = Column::where('id',$id)->first();
        if(!$col){
            return redirect('site', 301);
        }
        $son_list = Column::where('parent_id',$id)->get();
        $sites  = Site::where(['state'=>1])->whereIn('column_id',$son_list->pluck('id'))->orderBy('updated_at', 'desc')->paginate(10);
        return view('site.list',compact('nav_id','son_list','col','sites'));
    }
    public function listSonId($son,$id)
    {
        $nav_id = $id;
        $son_list = Column::where('parent_id',$id)->get();
        $col    = Column::where('id',$son)->first();
        $sites  = Site::where(['state'=>1,'column_id'=>$son])->orderBy('updated_at', 'desc')->paginate(10);
        return view('site.list',compact('nav_id','son_list','son','col','sites'));
    }
    public function show($id)
    {
        $site   = Site::where('id', $id)->first();
        if(!$site){
            return redirect('site', 301);
        }
        $site->increment('views');
        $col    = Column::where('id', $site->column_id)->first();
        $nav_id = $col->parent_id;
        $parent = Column::where('id', $col->parent_id)->first();
        $site_list = Site::where(['state'=>'1','column_id'=>$site->column_id])->orderBy('views','desc')->take('6')->get();
        $prev = $this->getPrevArticleId($id, $site->column_id);
        $next = $this->getNextArticleId($id, $site->column_id);
        return view('site.show',compact('nav_id','col','site','parent','site_list','prev','next'));
    }
    /**
     * 上一个
     */
    protected function getPrevArticleId($id,$cate_id){
        $aid = Site::where('id', '<', $id)->where('state',1)->where('column_id',$cate_id)->max('id');
        return Site::where('id','=',$aid)->first();
    }
    /**
     * 下一个
     */
    protected function getNextArticleId($id,$cate_id){
        $aid = Site::where('id', '>', $id)->where('state',1)->where('column_id',$cate_id)->min('id');
        return Site::where('id','=',$aid)->first();
    }
}
